package com.ll.springai.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RequestMapping("/chat/model")
@RequiredArgsConstructor
@RestController
public class ChatModelController {
    @Autowired
    private final OllamaChatModel ollamaChatModel;

    @GetMapping
    public String chat(@RequestParam("msg") String msg) {
        return ollamaChatModel.call(msg);
    }

    /**
     * Spring AI 支持 OpenAI 的 AI 语言模型 ChatGPT
     *
     * @param msg
     * @return
     */
    @GetMapping("/openai")
    public String openai(@RequestParam("msg") String msg) {
        ChatResponse call = ollamaChatModel.call(
            new Prompt(
                msg,
                OpenAiChatOptions.builder()//可以更换成其他大模型，如Anthropic3ChatOptions亚马逊
                    .withModel("gpt-3.5-turbo")
                    .withTemperature(0.8F)
                    .build()
            )
        );
        return call.getResult().getOutput().getContent();
    }

    /**
     * 流式响应
     *
     * @param msg
     * @return
     */
    @GetMapping(value = "/openai/stream", produces = "application/json;charset=UTF-8")
    public Flux<ChatResponse> stream(@RequestParam("msg") String msg) {
        return ollamaChatModel.stream(
            new Prompt(
                msg,
                OpenAiChatOptions.builder()//可以更换成其他大模型，如Anthropic3ChatOptions亚马逊
                    .withModel("gpt-3.5-turbo")
                    .withTemperature(0.8F)
                    .build()
            )
        );
    }

    @GetMapping("/prompt")
    public String prompt(@RequestParam("name") String name, @RequestParam("voice") String voice) {
        String userText = """
            给我推荐上海的至少三个旅游景点
            """;
        UserMessage userMessage = new UserMessage(userText);
        String systemText = """
            你是一个旅游咨询助手，可以帮助人们查询旅游信息。
            你的名字是{name},
            你应该用你的名字和{voice}的风格回复用户的请求。
            """;
        SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemText);
        //替换占位符
        Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", name, "voice", voice));
        Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
        List<Generation> results = ollamaChatModel.call(prompt).getResults();
        return results.stream().map(x -> x.getOutput().getContent()).collect(Collectors.joining(""));
    }
}

